﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Things.Bar.Models;
using Things.Bar;

namespace Things.Web.Bar
{
    public partial class SetHot : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                try
                {
                    Guid id = new Guid(Request["id"]);
                    string act = Request["act"];
                    if (act == "up")
                    {
                        new NoticeRepository().UpHotNotice(id);
                        Response.Redirect("./setHot.aspx");
                    }
                    else if (act == "down")
                    {
                        new NoticeRepository().DownHotNotice(id);
                        Response.Redirect("./setHot.aspx");
                    }
                    else
                    {
                        Response.Redirect("./setHot.aspx");
                    }
                }
                catch
                {
                    Response.Redirect("./setHot.aspx");
                }
                Response.Redirect("./setHot.aspx");
            }
            else if (!string.IsNullOrEmpty(Request["noticeid"]))
            {
                try
                {
                    Guid noticeId = new Guid(Request["noticeid"]);
                    NoticeRepository rep = new NoticeRepository();

                    List<HotNotice> hotNotices = rep.FindHotNotices();
                    if (hotNotices.Count < 10)
                    {
                        Notice originalNotice = rep.Find(noticeId);

                        HotNotice notice = new HotNotice();
                        notice.Id = Guid.NewGuid();
                        notice.NoticeId = noticeId;
                        notice.Title = originalNotice.Title;
                        notice.OrderIndex = hotNotices.Count;
                        rep.SaveHotNotice(notice);
                    }

                    Response.Redirect("./setHot.aspx");
                }
                catch
                {
                    Response.Redirect("./setHot.aspx");
                }
            }
            else
            {
                NoticeRepository rep = new NoticeRepository();
                List<HotNotice> hotNotices = rep.FindHotNotices();
                gridView.DataSource = hotNotices;
                gridView.DataBind();
            }
        }
    }
}